<template>
  <div class="user-bind-device">
    <div class="user-bind-device__title">用户绑定设备</div>
    <div class="user-bind-device__form">
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="产品ID:" prop="product_id">
          <el-input type="text" v-model="form.product_id"  placeholder="请输入产品ID"></el-input>
        </el-form-item>
        <el-form-item label="设备ID:" prop="device_id">
          <el-input type="text" v-model="form.device_id"  placeholder="请输入MAC"></el-input>
        </el-form-item>
        <el-form-item class="sumbit">
          <el-button type="danger" @click="submitForm" class="login-submit" >{{submiting ? '提交中...': '提交'}}</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import api from '@/api'
// import base64 from '@/utils/base64'

export default {
  name: 'UserBindDevice',
  data () {
    return {
      submiting: false,
      form: {
        product_id: '160fa6b3b69b03e9160fa6b3b69b6a01',
        device_id: '672984305'
      },
      qrcode: '',
      rules: {
        product_id: [
          { required: true, message: '请输入产品ID', trigger: 'blur' }
        ],
        device_id: [
          { required: true, message: '请输入设备ID', trigger: 'blur' }
        ]
      }
    }
  },

  computed: {
    userId () {
      return window.localStorage.userId
    }
  },
  methods: {
    submitForm () {
      if (this.submiting) return
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          let productId = this.form.product_id
          let deviceId = this.form.device_id
          let condition = {
            authority: 'R',
            custom_field: ['pid', 'sn'],
            format: {
              encode: 'base64'
            }
          }

          // 生成设备二维码
          let { data: { qrcode } } = await api.genQrcode(productId, deviceId, condition)
          this.qrcode = qrcode
          try {
            let params = {
              qrcode: qrcode
            }
            console.log(params)
            let res = await api.qrcode_subscribe(this.userId, params)
            console.log(res)
            console.log('绑定成功')
            this.$router.push('/xlink')
          } catch (err) {
            console.log('绑定失败' + err.response.data.error.msg)
          }
          this.submiting = false
        }
      })
    },

    async genQrcode () {
      let productId = this.form.product_id
      let deviceId = this.form.device_id
      let condition = {
        authority: 'R',
        custom_field: ['pid', 'sn'],
        format: {
          encode: 'base64'
        }
      }
      // 生成设备二维码
      let { data: { qrcode } } = await api.genQrcode(productId, deviceId, condition)
      this.qrcode = qrcode
    }
  }
}
</script>
<style lang="stylus" scoped>
.user-bind-device 
  width 500px
  border 1px solid #eee
  border-radius 12px
  margin 100px auto 1000px
  padding-bottom 20px
  box-shadow: 0 3px 6px rgba(0,0,0,.15)
  &__title
    font-size: 20px;
    margin 30px 0 25px
  &__form
    box-sizing border-box
    padding 0 80px
    .account-tip
      text-align left
    .message-tip
      text-align left
    .login-submit
      width 260px
      margin-top 5px
      border-radius 12px
    .user-bind-device-wrap
      a
        color #a5a4a4
        text-decoration none
</style>
